package com.trsvax.gwthello.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.i18n.client.Dictionary;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.google.gwt.visualization.client.visualizations.AnnotatedTimeLine;
import com.google.gwt.visualization.client.visualizations.Gauge;
import com.google.gwt.visualization.client.visualizations.LineChart;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public abstract class GWTTapestry implements EntryPoint {

	/**
	 * This is the entry point method.
	 */

	public void onModuleLoad() {
		Dictionary config = Dictionary.getDictionary(getName());
		String[] ids = config.get("componentIDs").split(",");
		if (ids == null)
			return;

		Runnable onLoadCallback = new myRunnable(ids);

		VisualizationUtils.loadVisualizationApi(onLoadCallback, AnnotatedTimeLine.PACKAGE, LineChart.PACKAGE,
				Gauge.PACKAGE);

		Window.alert("loading");
		for (String id : ids) {
			Composite w = widgetFactory(id);
			if (w != null)
				RootPanel.get(id).add(w);
		}
	}

	class myRunnable implements Runnable {
		private String[] ids;

		public myRunnable(String[] ids) {
			this.ids = ids;
		}

		public void run() {
			for (String id : ids) {
				Composite w = widgetFactory(id);
				if (w != null)
					RootPanel.get(id).add(w);
			}

		}

	}

	public String getName() {
		return this.getClass().getName().replace(".", "");
	}

	public abstract Composite widgetFactory(String id);
}
